﻿<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="opAdd">新增</el-button>
                <!-- 酒业导入 -->
                <!-- <sc-file-import
                    v-if="orgType==1&&(orgName=='兔宝贝'||orgName=='人从众')"
                    ref="scFileImport"
                    @fatherMethod="opBindData"
                    :importUrl="'apply-order/import'"
                    :templateUrl="'https://oss.jtmckj.com/Supply/ApplyOrder/20240821/60188f17405d43dc98258bd35b13dbab.xlsx'"
                ></sc-file-import>-->
                <el-button
                    v-if="orgType==1"
                    type="primary"
                    icon="el-icon-plus"
                    @click="opBatchTask"
                >门店批量收货</el-button>
                <!-- 门店导入 v-if="orgType==2||(orgType==1&&orgName=='兔宝贝供应链')" -->
                <el-button type="primary" icon="el-icon-plus" @click="openImportSave">门店导入</el-button>
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    v-if="orgType==1"
                    @click="openAntiAudit"
                >反审核</el-button>
                <el-dropdown>
                    <el-button type="primary">
                        下查
                        <el-icon class="el-icon--right">
                            <arrow-down />
                        </el-icon>
                    </el-button>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item @click="goDownOrder('purchase')">采购订单</el-dropdown-item>
                            <el-dropdown-item @click="goDownOrder('receive')">销售出库单</el-dropdown-item>
                            <el-dropdown-item @click="goDownOrder('applyDeliveryFee')">销售配送单</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
                <span>&nbsp;&nbsp;</span>
                <sc-export
                    :queryType="'apply-order/export'"
                    :queryList="queryList"
                    v-if="orgType==1"
                    :selectedKeys="selectedKeys"
                    :fileName="'导出'"
                />
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    v-if="orgType==1"
                    @click="openUpdatePrice"
                >价格修改</el-button>
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    v-if="orgType==1"
                    @click="openOperateLogs"
                >操作记录</el-button>
                <el-dropdown>
                    <el-button type="primary" v-if="orgType==1">
                        <el-icon>
                            <Money />&nbsp;
                        </el-icon>费用
                        <el-icon class="el-icon--right">
                            <arrow-down />
                        </el-icon>
                    </el-button>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item @click="opFee('delivery')">
                                <el-icon>
                                    <Money />
                                </el-icon>配送费
                            </el-dropdown-item>
                            <el-dropdown-item @click="opFee('deposit')">
                                <el-icon>
                                    <Money />
                                </el-icon>押金费
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
                <span>&nbsp;&nbsp;</span>
                <el-button type="primary" icon="el-icon-close" v-if="orgType==1" @click="opStop">终止</el-button>
            </div>
            <div class="right-panel">
                <el-button :icon="Search"   type="primary" @click="opBindData">查询</el-button>
                <el-button :icon="Search" type="info" plain @click="resetSearch">重置</el-button>
                <el-button text type="primary" @click="showCondition = !showCondition">
                    <span v-if="!showCondition">展开</span>
                    <span v-else>收起</span>
                    <vab-icon
                        class="vab-dropdown"
                        :class="{ 'vab-dropdown-active': fold }"
                        icon="arrow-up-s-line"
                    />
                </el-button>
            </div>
        </el-header>
        <el-foot v-show="showCondition">
            <vab-query-form-top-panel>
                <el-form
                    inline
                    label-width="49px"
                    :model="queryForm"
                    style="margin-top: 5px;margin-right: 20px;"
                >
                    <el-row type="flex" style="flex-wrap:wrap">
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="要货编号" label-width="100px" style="width: 100%">
                                <el-input
                                    placeholder="---请输入要货编号---"
                                    v-model="queryList.applyOrderCode"
                                    style="width: 100%"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="采购单位" label-width="100px" style="width: 100%">
                                <sc-auto-complete
                                    :orgType="1"
                                    :queryType="'purchase-unity/search'"
                                    :placeholder="'---请输入采购单位---'"
                                    v-model="queryList.purchaseUnityName"
                                    @handleSelect="(e)=>{this.queryList.purchaseUnityName=e.purchaseUnityName}"
                                    style="width: 100%"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="门店名称" label-width="100px" style="width: 100%">
                                <sc-auto-complete
                                    :orgType="2"
                                    :queryType="'store/search'"
                                    :placeholder="'---请输入申请单位---'"
                                    v-model="queryList.storeName"
                                    @handleSelect="(e)=>{this.queryList.storeName=e.storeName}"
                                    style="width: 100%"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="要货日期" label-width="100px" style="width: 100%">
                                <el-date-picker
                                    style="width: 100%"
                                    v-model="queryList.applyDates"
                                    type="datetimerange"
                                    value-format="YYYY-MM-DD HH:mm:ss"
                                    format="YYYY-MM-DD HH:mm:ss"
                                    range-separator="-"
                                    unlink-panels="true"
                                    start-placeholder="开始时间"
                                    end-placeholder="结束时间"
                                    :default-time="defaultTime"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="物料编码" label-width="100px" style="width: 100%">
                                <el-input
                                    placeholder="---请输入物料编码---"
                                    v-model="queryList.materialCode"
                                    style="width: 100%"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="物料名称" label-width="100px" style="width: 100%">
                                <el-input
                                    placeholder="---请输入物料名称---"
                                    v-model="queryList.materialName"
                                    style="width: 100%"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="状态" label-width="100px" style="width: 100%">
                                <el-select
                                    placeholder="---请选择状态---"
                                    v-model="queryList.statusList"
                                    style="width: 100%"
                                    collapse-tags
                                    collapse-tags-tooltip
                                    multiple
                                >
                                    <el-option label="---请选择---" value />
                                    <el-option
                                        v-for="item in optionsState"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="期望到货日期" label-width="100px" style="width: 100%">
                                <el-date-picker
                                    style="width: 100%"
                                    v-model="queryList.expectedDates"
                                    type="datetimerange"
                                    value-format="YYYY-MM-DD HH:mm:ss"
                                    format="YYYY-MM-DD HH:mm:ss"
                                    range-separator="-"
                                    unlink-panels="true"
                                    start-placeholder="开始时间"
                                    end-placeholder="结束时间"
                                    :default-time="defaultTime"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="业务员" label-width="100px" style="width: 100%">
                                <el-input
                                    placeholder="---请输入业务员---"
                                    v-model="queryList.auditName"
                                    style="width: 100%"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="创建人" label-width="100px" style="width: 100%">
                                <el-input
                                    placeholder="---请输入创建人---"
                                    v-model="queryList.crName"
                                    style="width: 100%"
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="收货状态" label-width="100px" style="width: 100%">
                                <el-select
                                    placeholder="---请选择收货状态---"
                                    v-model="queryList.taskStatusList"
                                    style="width: 100%"
                                    collapse-tags
                                    collapse-tags-tooltip
                                    multiple
                                >
                                    <el-option label="---请选择---" value />
                                    <el-option
                                        v-for="item in optionsTaskState"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="创建时间" label-width="100px" style="width: 100%">
                                <el-date-picker
                                    style="width: 100%"
                                    v-model="queryList.crDates"
                                    type="datetimerange"
                                    value-format="YYYY-MM-DD HH:mm:ss"
                                    format="YYYY-MM-DD HH:mm:ss"
                                    range-separator="-"
                                    unlink-panels="true"
                                    start-placeholder="开始时间"
                                    end-placeholder="结束时间"
                                    :default-time="defaultTime"
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <!-- <el-form-item label="最后修改人" label-width="100px">
                        <el-input
                            placeholder="---请输入名称---"
                            v-model="queryList.edName"
                            style="width: 300px"
                        />
                    </el-form-item>
                    <el-form-item label="最后修改时间" label-width="100px">
                        <el-date-picker
                            style="width: 300px"
                            v-model="queryList.edDates"
                            type="datetimerange"
                            value-format="YYYY-MM-DD HH:mm:ss"
                            format="YYYY-MM-DD HH:mm:ss"
                            range-separator="-"
                            unlink-panels="true"
                            start-placeholder="开始时间"
                            end-placeholder="结束时间"
                        />
                    </el-form-item>-->
                </el-form>
            </vab-query-form-top-panel>
        </el-foot>

        <el-main class="nopadding">
            <scTable
                ref="mytable"
                :queryType="'apply-order/distributor-pages'"
                tableName="applyOrder"
                :data="listData"
                :queryList="queryList"
                :column="columnList"
                :orderList="OrderList"
                :sort-orders="['ascending', 'descending']"
                @selection-change="selectionChange"
                row-key="applyOrderId"
                :row-class-name="setRowClass"
                :isKeyDown="showCondition"
                stripe
                border
                show-summary
                remoteSummary
                :columnDrop="true"
            >
                <el-table-column type="selection" width="50"></el-table-column>
                <!-- <el-table-column prop="applyDate" label="申请时间" width="150" sortable>
                    <template #default="scope">{{scope.row.applyDate?.slice(0,10)}}</template>
                </el-table-column>-->
                <!-- <el-table-column prop="applyDate" label="要货时间" width="150" sortable>
				    <template #default="scope">{{scope.row.applyDate?.slice(0,10)}}</template>
				</el-table-column>
                <el-table-column prop="storeName" label="要货单位" width="140" sortable></el-table-column>
                <el-table-column prop="statusDesc" label="单据状态" width="140" sortable></el-table-column> 

                <el-table-column prop="expectedDate" label="期望到货时间" width="150" sortable>
                    <template #default="scope">{{scope.row.expectedDate?scope.row.expectedDate?.slice(0,10):""}}</template>
                </el-table-column>
                <el-table-column prop="applyOrderCode" label="要货编号" width="160" sortable>
                    <template #default="scope"> <span class="el-span-text" @click="opShow(scope.row, scope.$index)">{{scope.row.applyOrderCode}}</span></template>
                </el-table-column>
                <el-table-column prop="materialDesc" label="要货物料" width="240" sortable show-overflow-tooltip></el-table-column>
                <el-table-column prop="totalAmount" label="单据金额" width="140" sortable>
                    <template #default="scope">{{scope.row.totalAmount?.toFixed(2)}}</template>
                </el-table-column>
                <el-table-column prop="sendStatusDesc" label="发货状态" width="150" sortable>
                    <template #default="scope">
                        {{ scope.row.sendStatusDesc }}
                    </template>

                </el-table-column>
                <el-table-column prop="taskStatusDesc" label="收货状态" width="150" sortable>
                    <template #default="scope">
                         <span v-if="scope.row.sendStatusDesc=='部分出库发货'">{{ scope.row.taskStatusDesc }}</span>
                        <span v-else>/</span>
                    </template>
                </el-table-column>
                <el-table-column prop="remarks" label="备注" width="220" sortable></el-table-column>
                <el-table-column prop="auditRemarks" label="审核备注" width="220" sortable></el-table-column>
                <el-table-column prop="applyName" label="申请人" width="140" sortable></el-table-column>

                <el-table-column prop="purchaseUnityName" label="采购单位" width="160" sortable v-if="orgType==1"></el-table-column>
                <el-table-column prop="purchaseUnityName" label="供货单位" width="160" sortable v-if="orgType==2"></el-table-column>
                -->
                <template #applyDate="{row}">{{row.applyDate?.slice(0,10)}}</template>
                <template #auditDate="{row}">{{row.auditDate?row.auditDate?.slice(0,10):""}}</template>
                <template #applyOrderCode="{row}">
                    <span class="el-span-text" @click="opShow(row)">{{row.applyOrderCode}}</span>
                </template>
                <template #materialDesc="{row}">
                    <el-popover placement="bottom-start" :width="800"   trigger="click">
                        <template #reference>
                            <div
                                style="position: relative;text-overflow: ellipsis;white-space: nowrap;cursor: pointer;display: block;"
                            >
                                <span
                                    v-for="item in row.materialDescs"
                                    :key="item"
                                    style="color: #318cf3;"
                                >
                                    <!-- <el-image style="width: 40px; height: 35px;vertical-align: middle;" :src="item.materialImage" :fit="cover" />  -->
                                    {{item.materialName}}/
                                    <span v-if="item.receiveQty<=0">-</span>
                                    <span v-else>{{item.receiveQty}}</span>
                                    {{item.unitName}};
                                </span>
                            </div>
                        </template>
                        <!-- <div
                            style="text-align: center;margin: 10px;font-size: 16px;font-weight: bold;"
                        >物料明细</div>-->
                        <el-table :data="row.materialDescs" :style="row.materialDescs?.length>6?'height: 300px;width: 100%;':'width: 100%;'" stripe :row-class-name="setMaterialRowClass">
                            <el-table-column prop="rowNumber" label="序号" width="60" />
                            <!-- <el-table-column prop="materialImage" label="图片" width="70">
                                <template #default="scope1">
                                    <el-image
                                        style="width: 45px; height: 40px"
                                        :src="scope1.row.materialImage"
                                        :fit="cover"
                                    />
                                </template>
                            </el-table-column>-->
                            <el-table-column prop="materialName" label="物料名称" min-width="120"/>
                            <el-table-column prop="materialSpecs" label="物料规格" min-width="120"/>
                   
                            <el-table-column prop="qty" label="要货" min-width="80" />
                            <el-table-column prop="supplierName" label="供应商" min-width="140" />
                            <el-table-column prop="receiveQty" label="供应商接单" min-width="90">
                                <template #default="scope">
                                    <span v-if="scope.row.receiveStatus==0">/</span>
                                    <span v-else>{{scope.row.receiveQty}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="unitName" label="单位" min-width="80" />
                        </el-table>
                    </el-popover>
                </template>
                <template #totalAmount="{row}">{{row.totalAmount?.toFixed(2)}}</template>
                <template #deliveryAmount="{row}">{{row.isAllDelivery ?row.deliveryAmount?.toFixed(2):'/'}}</template>

                <template #status="{row}">
                    <span>{{ row.statusDesc }}</span>
                </template>
                <template #taskStatusDesc="{row}">
                    <span v-if="row.status!=5&&row.status!=6&&row.status!=8">/</span>
                    <span v-else>{{ row.taskStatusDesc }}</span>
                </template>
                <template #applyFile="{row}">
                    <el-link type="primary" @click="opShowFile(row)">查看附件</el-link>
                </template>
                <template #sourcePlatformType="{row}">{{ row.sourcePlatformTypeDesc }}</template>

                <el-table-column label="操作" fixed="right" align="center" width="250">
                    <template #default="scope">
                        <el-button-group>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="opShow(scope.row, scope.$index)"
                            >查看</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="opReceive(scope.row, scope.$index)"
                                v-if="orgType==1&&(scope.row.status==4||scope.row.status==5)"
                            >接单</el-button>
                            <!-- <el-button
                                text
                                type="primary"
                                size="small"
                                v-show="(scope.row.sendStatus =='1' || scope.row.sendStatus =='2') && scope.row.taskStatus!=2 &&scope.row.status!=9"
                                @click="opTask(scope.row, scope.$index)"
                            >收货</el-button> -->
                            <el-button
                                text
                                type="primary"
                                size="small"
                                v-show="scope.row.purchaseOrderStatus >=3 && scope.row.taskStatus!=2 &&scope.row.status!=9"
                                @click="opTask(scope.row, scope.$index)"
                            >收货</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                v-show="scope.row.taskStatus>0&&scope.row.status>4"
                                @click="opTaskItem(scope.row, scope.$index)"
                            >收货明细</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                v-if="scope.row.status<=3"
                                @click="opEdit(scope.row, scope.$index)"
                            >编辑</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                v-if="orgType==2"
                                @click="opCopy(scope.row, scope.$index)"
                            >复制</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="setStatus(scope.row, 2)"
                                v-if="orgType==2&&scope.row.status==4"
                            >退回</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="setStatus(scope.row, 2)"
                                v-if="orgType==2&&scope.row.status==4"
                            >撤回</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="setStatus(scope.row, 4)"
                                v-if="orgType==2&&scope.row.status==3"
                            >提交</el-button>
                            <sc-export
                                v-if="orgType==1&&scope.row.status==20&&scope.row.applyOrderType==2"
                                :queryType="`apply-order/down-file/${scope.row.applyOrderId}`"
                                :queryList="queryList"
                                :fileName="`要货订单_${scope.row.applyOrderCode}`"
                                :isTest="true"
                                :isText="true"
                                :showText="下载文件"
                            />
                            <sc-file-import
                                ref="scFileImport"
                                v-if="orgType==1&&scope.row.status==20&&scope.row.applyOrderType==2"
                                :isTest="true"
                                :isText="true"
                                @fatherMethod="opBindData"
                                :importUrl="`apply-order/${scope.row.applyOrderId}/import-create-apply`"
                            ></sc-file-import>
                            <!-- <el-button
                                text
                                type="primary"
                                size="small"
                                v-show="orgType==1 &&scope.row.status==21"
                                @click="opAudit(scope.row, scope.$index)"
                            >审核</el-button>-->
                        </el-button-group>
                        <!-- <el-dropdown trigger="click">
                            <span>
                                更多
                                <el-icon>
                                    <arrow-down />
                                </el-icon>
                            </span>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item
                                        @click="opEdit(scope.row, scope.$index)"
                                        v-if="scope.row.status<=3"
                                    >
                                        <el-icon>
                                            <EditPen />
                                        </el-icon>编辑
                                    </el-dropdown-item>
                                    <el-dropdown-item
                                        @click="opCopy(scope.row, scope.$index)"
                                        v-if="orgType==2"
                                    >
                                        <el-icon>
                                            <Connection />
                                        </el-icon>复制
                                    </el-dropdown-item>
                                    <el-dropdown-item
                                        @click="setStatus(scope.row, 1)"
                                        v-if="orgType==1&&scope.row.status==4&&scope.row.applyOrderType!=2"
                                    >
                                        <el-icon>
                                            <RefreshLeft />
                                        </el-icon>退回
                                    </el-dropdown-item>
                                    <el-dropdown-item
                                        @click="setStatus(scope.row, 2)"
                                        v-if="orgType==2&&scope.row.status==4"
                                    >
                                        <el-icon>
                                            <RefreshLeft />
                                        </el-icon>撤回
                                    </el-dropdown-item>
                                    <el-dropdown-item
                                        @click="setStatus(scope.row, 4)"
                                        v-if="orgType==2&&scope.row.status==3"
                                    >
                                        <el-icon>
                                            <Select />
                                        </el-icon>提交
                                    </el-dropdown-item>
                                    <el-dropdown-item
                                        @click="setStatus(scope.row, 9)"
                                        v-if="(orgType==1&&(scope.row.status!=8&&scope.row.status!=9)||orgType==2&&(scope.row.status!=5&&scope.row.status!=6&&scope.row.status!=8&&scope.row.status!=9))"
                                    >
                                        <el-icon>
                                            <CloseBold />
                                        </el-icon>终止
                                    </el-dropdown-item>

                                    <el-dropdown-item
                                        @click="opReceive(scope.row, scope.$index)"
                                        v-if="orgType==1&&(scope.row.status==4||scope.row.status==5)"
                                    >
                                        <el-icon>
                                            <DocumentAdd />
                                        </el-icon>接单
                                    </el-dropdown-item>
                                    <el-dropdown-item
                                        @click="opDeliveryFee(scope.row)"
                                        v-if="orgType==1&&(scope.row.status==5||scope.row.status==6||scope.row.status==8)"
                                    >
                                        <el-icon>
                                            <Van />
                                        </el-icon>配送费
                                    </el-dropdown-item>
                                    <el-dropdown-item
                                        @click="opDepositFee(scope.row)"
                                        v-if="orgType==1&&(scope.row.status==5||scope.row.status==6||scope.row.status==8)"
                                    >
                                        <el-icon>
                                            <Money />
                                        </el-icon>押金费
                                    </el-dropdown-item>
                                    <el-dropdown-item @click="opAudit(scope.row)" v-if="orgType==1">
                                        <el-icon>
                                            <Money />
                                        </el-icon>审核
                                    </el-dropdown-item>
                                    <el-dropdown-item
                                        v-if="orgType==1&&scope.row.status==20&&scope.row.applyOrderType==2"
                                    >
                                        <el-icon>
                                            <Download />
                                        </el-icon>
                                        <sc-export
                                            :queryType="`apply-order/down-file/${scope.row.applyOrderId}`"
                                            :queryList="queryList"
                                            :fileName="`要货订单_${scope.row.applyOrderCode}`"
                                            :isTest="true"
                                            :isText="true"
                                            :showText="下载文件"
                                        />
                                    </el-dropdown-item>
                                    <el-dropdown-item
                                        v-if="orgType==1&&scope.row.status==20&&scope.row.applyOrderType==2"
                                    >
                                        <el-icon>
                                            <Upload />
                                        </el-icon>
                                        <sc-file-import
                                            ref="scFileImport"
                                            :isTest="true"
                                            :isText="true"
                                            @fatherMethod="opBindData"
                                            :importUrl="`apply-order/${scope.row.applyOrderId}/import-create-apply`"
                                        ></sc-file-import>
                                    </el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>-->
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
    <!-- fullscreen -->
    <save-dialog
        v-if="dialogSave"
        ref="saveDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogSave = false"
    ></save-dialog>

    <receive-dialog
        v-if="receiveSave"
        ref="receiveDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="receiveSave = false"
    ></receive-dialog>

    <detail-dialog
        v-if="dialogDetail"
        ref="detailDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogDetail = false"
    ></detail-dialog>

    <task-dialog
        v-if="dialogTask"
        ref="taskDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogTask = false"
    ></task-dialog>

    <task-item-dialog
        v-if="dialogTaskItem"
        ref="dialogTaskItem"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogTaskItem = false"
    ></task-item-dialog>

    <file-dialog
        v-if="dialogFile"
        ref="fileDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogFile = false"
    ></file-dialog>

    <!-- 门店导入 -->
    <import-save-dialog
        v-if="importSaveState"
        ref="importSaveDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="importSaveState = false"
    ></import-save-dialog>
    <!-- 配送费 -->
    <delivery-fee-dialog
        v-if="dialogDeliveryFee"
        ref="deliveryFeeDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogDeliveryFee = false"
    ></delivery-fee-dialog>
    <!-- 押金费 -->
    <deposit-fee-dialog
        v-if="dialogDepositFee"
        ref="depositFeeDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogDepositFee = false"
    ></deposit-fee-dialog>
    <!-- 审核 -->
    <audit-dialog
        v-if="dialogAudit"
        ref="auditDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogAudit = false"
    ></audit-dialog>
    <!-- 操作日志列表 -->
    <!-- <operate-Logs-dialog
        v-if="dialogOperateLogs"
        ref="operateLogsDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogOperateLogs = false"
    ></operate-Logs-dialog> -->
    <!-- 操作日志列表 -->
    <operateLogs v-if="dialogOperateLogs" ref="operateLogsDialog" @closed="dialogOperateLogs = false"></operateLogs>
    <!-- 要货价格修改 -->
    <update-price-dialog
        v-if="dialogUpdatePrice"
        ref="updatePriceDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogUpdatePrice = false"
    ></update-price-dialog>
</template>

<script>
import updatePriceDialog from "../order/updatePrice.vue";
import operateLogsDialog from "../order/operateLogs.vue";
import taskDialog from "../../taskOrder/task";
import taskItemDialog from "../../taskOrder/taskItem";
import detailDialog from "../order/detail";
import saveDialog from "./save";
import importSaveDialog from "../order/importSave";
import receiveDialog from "../order/receive";
import fileDialog from "../order/attachment";
import publicjs from "@/api/model/publicjs";
import tool from "@/utils/tool";
import deliveryFeeDialog from "../order/deliveryFee";
import depositFeeDialog from "../order/depositFee";
import auditDialog from "../order/audit";
import config from "@/config";
export default {
    name: "applyOrder",
    components: {
        updatePriceDialog,
        operateLogsDialog,
        taskDialog,
        taskItemDialog,
        detailDialog,
        saveDialog,
        receiveDialog,
        fileDialog,
        importSaveDialog,
        deliveryFeeDialog,
        depositFeeDialog,
        auditDialog,
        publicjs,
    },
    data() {
        return {
            dialogTask: false,
            dialogTaskItem: false,
            importState: false,
            importSaveState: false,
            dialogDetail: false,
            dialogSave: false,
            dialogFile: false,
            receiveSave: false,
            showCondition: false,
            dialogDeliveryFee: false, //配送费
            dialogDepositFee: false, //押金费
            dialogAudit: false, //审核
            dialogUpdatePrice:false,//修改要货价格
            dialogOperateLogs:false,//操作日志记录
            selectionRows: [],
            pkList: ["applyOrderId"],
            columnList: [
                {
                    prop: "applyDate",
                    label: "要货时间",
                    width: "150",
                    sortable: true,
                },
                {
                    prop: "storeName",
                    label: "要货单位",
                    width: "140",
                    sortable: true,
                },
                {
                    prop: "status",
                    label: "单据状态",
                    width: "140",
                    sortable: true,
                },
                {
                    prop: "auditDate",
                    label: "供货单位接单时间",
                    width: "160",
                    sortable: true,
                },
                {
                    prop: "applyOrderCode",
                    label: "要货编号",
                    width: "160",
                    sortable: true,
                },
                {
                    prop: "materialDesc",
                    label: "要货物料",
                    width: "240",
                },
                {
                    prop: "totalAmount",
                    label: "要货金额",
                    width: "140",
                    sortable: true,
                },
                {
                    prop: "deliveryAmount",
                    label: "销售金额",
                    width: "140",
                    sortable: true,
                },
                { prop: "taskStatusDesc", label: "收货状态", width: "140" },
                { prop: "auditRemarks", label: "审核备注", width: "220" },
                {
                    prop: "applyName",
                    label: "申请人",
                    width: "140",
                    sortable: true,
                },
                {
                    prop: "purchaseUnityName",
                    label: "供货单位",
                    width: "120",
                    sortable: true,
                },
                {
                    prop: "applyFile",
                    label: "附件",
                    width: "120",
                    sortable: true,
                    hide: this.orgType == 1,
                },
                {
                    prop: "sourcePlatformType",
                    label: "下单平台",
                    width: "120",
                    sortable: true,
                    hide: this.orgType == 1,
                },
            ],
            OrderList: { applyOrderId: "Asc" },
            queryList: {},
            result: "", // 运行结果
            props1: {
                checkStrictly: true,
                emitPath: false,
            },
            props: {
                label: "storeName",
                value: "storeId",
            },
            purchaseUnityProps: {
                label: "purchaseUnityName",
                value: "purchaseUnityId",
            },
            storeAutoCompleteProps: {
                label: "storeName",
                value: "storeId",
            },
            addState: true, //可以增加
            editState: true, //可以修改
            editBatchState: true, //可以批量修改
            deleteState: true, //可以删除
            optionsState: [],
            optionsSendState: [], //发货状态
            optionsTaskState: [], //收货状态
            materialTypeTree: [],
            orgType: 0,
            selectedRow: [],
            defaultTime: [
                new Date(2000, 1, 1, 0, 0, 0),
                new Date(2000, 2, 1, 23, 59, 59),
            ],
            selectedKeys: [], //选中的ID
        };
    },
    methods: {
        //收货
        opTask(row) {
            this.dialogTask = true;
            this.$nextTick(() => {
                this.$refs.taskDialog.open("Task").setData([
                    {
                        orderCode: row.applyOrderCode,
                        storeId: row.storeId,
                        storeName: row.storeName,
                        crName: row.applyName,
                        crDate: row.applyDate,
                        expectedDate: row.expectedDate,
                        taskStatusDesc: row.taskStatusDesc,
                        taskOrderType: 0,
                        orderId: row.applyOrderId,
                    },
                ]);
            });
        },
        //批量收货
        opBatchTask() {
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要收货的订单");
                return;
            }
            var data = [];
            this.selectionRows.map((row) =>
                data.push({
                    orderCode: row.applyOrderCode,
                    storeId: row.storeId,
                    storeName: row.storeName,
                    crName: row.applyName,
                    crDate: row.applyDate,
                    expectedDate: row.expectedDate,
                    taskStatusDesc: row.taskStatusDesc,
                    taskOrderType: 0,
                    orderId: row.applyOrderId,
                })
            );
            this.dialogTask = true;
            this.$nextTick(() => {
                this.$refs.taskDialog.open("Task").setData(data);
            });
        },
        //查看
        opTaskItem(row) {
            this.dialogTaskItem = true;
            this.$nextTick(() => {
                this.$refs.dialogTaskItem.open("Task").setData({
                    orderCode: row.applyOrderCode,
                    storeId: row.storeId,
                    storeName: row.storeName,
                    crName: row.applyName,
                    crDate: row.applyDate,
                    expectedDate: row.expectedDate,
                    taskStatusDesc: row.taskStatusDesc,
                    taskOrderType: 0,
                    orderId: row.applyOrderId,
                    status: row.status,
                });
            });
        },
        opImport() {
            this.importState = true;
        },
        //门店导入
        openImportSave() {
            this.importSaveState = true;
            this.$nextTick(() => {
                this.$refs.importSaveDialog.setData({});
                this.$refs.importSaveDialog.open("Add");
            });
        },
        //添加
        opAdd() {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.setData({});
                this.$refs.saveDialog.open("Add");
            });
        },
        //编辑
        opEdit(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.setData(row);
                this.$refs.saveDialog.open("Edit");
            });
        },
        //复制
        opCopy(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.setData({ ...row, applyOrderCode: "" });
                this.$refs.saveDialog.open("Add");
            });
        },
        //查看
        opShow(row) {
            this.dialogDetail = true;
            this.$nextTick(() => {
                this.$refs.detailDialog.setData(row);
                this.$refs.detailDialog.open("Show");
            });
        },
        //接单
        opReceive(row) {
            this.receiveSave = true;
            this.$nextTick(() => {
                this.$refs.receiveDialog.setData(row);
                this.$refs.receiveDialog.open("Add");
            });
        },
        //配送费
        async opDeliveryFee(row) {
            this.dialogDeliveryFee = true;
            this.$nextTick(() => {
                this.$refs.deliveryFeeDialog.setData(row);
                this.$refs.deliveryFeeDialog.open("deliveryFee");
            });
        },
        //押金费
        async opDepositFee(row) {
            this.dialogDepositFee = true;
            this.$nextTick(() => {
                console.log("row",row)
                this.$refs.depositFeeDialog.setData(row);
                this.$refs.depositFeeDialog.open("depositFee");
            });
        },
        //修改要货价格
        async openUpdatePrice() {
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要操作的订单");
                return;
            }

            if (this.selectionRows.length > 1) {
                this.$message.error("只能选择一项进行操作！");
                return;
            }
            this.dialogUpdatePrice = true;
            this.$nextTick(() => {
                this.$refs.updatePriceDialog.setData(this.selectionRows[0]);
                this.$refs.updatePriceDialog.open("updatePrice");
            });
        },
        //操作日志
        async openOperateLogs() {
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要操作的订单");
                return;
            }
            const applyOrderIds=this.selectionRows.map(p=>p.applyOrderId);
            this.dialogOperateLogs = true;
            console.log(this.dialogOperateLogs);
            this.$nextTick(() => {
                this.$refs.operateLogsDialog.setData({operateOrderType:0, operateOrderIds:applyOrderIds});
                this.$refs.operateLogsDialog.open("operateLog");
            });
        },
        //操作金额
        async opFee(type) {
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要操作的订单");
                return;
            }

            if (this.selectionRows.length > 1) {
                this.$message.error("只能选择一项进行操作！");
                return;
            }
            if (type == "delivery") {
                this.opDeliveryFee(this.selectionRows[0]);
            } else if (type == "deposit") {
                this.opDepositFee(this.selectionRows[0]);
            }
        },
        //终止
        async opStop() {
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要操作的订单");
                return;
            }

            if (this.selectionRows.length > 1) {
                this.$message.error("只能选择一项进行操作！");
                return;
            }
            var row = this.selectionRows[0];
            if (
                (row.status != 8 && row.status != 9) ||
                (orgType == 2 &&
                    row.status != 5 &&
                    row.status != 6 &&
                    row.status != 8 &&
                    row.status != 9)
            ) {
                this.setStatus(row, 9);
            } else {
                this.$message.error("暂无权限！");
                return;
            }
        },
        //审核
        async opAudit(row) {
            this.dialogAudit = true;
            this.$nextTick(() => {
                this.$refs.auditDialog.setData(row);
                this.$refs.auditDialog.open("audit");
            });
        },
        //设置状态
        async setStatus(row, status) {
            let url = "apply-order/submitted";
            switch (status) {
                case 1:
                    url = "apply-order/returned";
                    break;
                case 2:
                    url = "apply-order/withdrawn";
                    break;
                case 4:
                    url = "apply-order/submitted";
                    break;
                case 9:
                    url = "apply-order/close";
                    break;
            }

            //终止
            if (status == 9) {
                if (row.status == 5 || row.status == 6) {
                    try {
                        await this.$confirm(
                            `该订单已接单，确认要终止该要货订单吗？`,
                            "提示",
                            {
                                type: "warning",
                            }
                        );
                    } catch (error) {
                        // 用户点击取消时，执行的操作
                        console.log("取消操作");
                        return;
                    }
                }
            }

            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    url,
                    { applyOrderIds: [row.applyOrderId], remarks: row.remarks }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        setRowClass({ row, rowIndex }) {
            let rowClass = "";
            if (this.selectionRows.includes(row)) {
                rowClass += " selected-row-color";
            }

            //终止
            if (row.status == 9) {
                rowClass += " selected-row-color-grey";
            }
            return rowClass;
        },
        setMaterialRowClass({ row, rowIndex }) {
            let rowClass = "";
            //终止
            if (row.receiveStatus == 9) {
                rowClass += " selected-row-color-grey";
            }
            return rowClass;
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selectionRows = selection;
            console.log("selection", selection);
            this.selectedKeys = this.selectionRows.map((item) => {
                return item.applyOrderId;
            });
        },
        //本地更新数据
        handleSaveSuccess(data, mode) {
            if (mode == "Add") {
                this.$refs.mytable.refresh();
            } else if (mode == "Edit") {
                this.$refs.mytable.refresh();
            }
        },
        async opBindData() {
            this.$refs.mytable.clearSort();
            this.$refs.mytable.getData();
        },
        resetSearch() {
            this.queryList = {};
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    {
                        Codes: [
                            "ApplyOrderStatus",
                            "PurchaseOrderSendStatus",
                            "TaskStatus",
                        ],
                    }
                );
                if (returnValue.status == 200) {
                    var applyOrderStatus = returnValue.response.find(
                        (p) => p.code == "ApplyOrderStatus"
                    ).optionsDetails;
                    this.optionsSendState = returnValue.response.find(
                        (p) => p.code == "PurchaseOrderSendStatus"
                    ).optionsDetails;
                    this.optionsTaskState = returnValue.response.find(
                        (p) => p.code == "TaskStatus"
                    ).optionsDetails;
                    if (this.orgType == 1) {
                        this.optionsState = applyOrderStatus;
                        //this.optionsState=applyOrderStatus.filter((p) => p.value != 3);
                    } else {
                        this.optionsState = applyOrderStatus;
                    }
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        async orgSelectProcess(rowData) {
            this.queryList.stores = [];
            this.queryList.storeIds = [];

            this.queryList.stores = rowData;
            if (rowData && rowData.length > 0) {
                this.queryList.storeIds = rowData.map((p) => {
                    return p.storeId;
                });
            }
            await this.opBindData();
        },
        async purchaseUnitySelectProcess(rowData) {
            this.queryList.purchaseUnitys = [];
            this.queryList.purchaseUnityIds = [];

            this.queryList.purchaseUnitys = rowData;
            if (rowData && rowData.length > 0) {
                this.queryList.purchaseUnityIds = rowData.map((p) => {
                    return p.purchaseUnityId;
                });
            }
            await this.opBindData();
        },
        //门店选择事件
        handleStoreSelect(data) {
            console.log("选中", data);
            this.queryList.storeName = data.storeName;
        },
        //查看附件
        opShowFile(row) {
            this.dialogFile = true;
            this.$nextTick(() => {
                this.$refs.fileDialog.setData(row);
                this.$refs.fileDialog.open("Show");
            });
        },
        //下查订单
        goDownOrder(type) {
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要下查的要货订单");
                return;
            }

            if (this.selectionRows.length > 1) {
                this.$message.error("只能选择一条要下查的要货订单");
                return;
            }

            switch (type) {
                case "purchase":
                    this.$router.push({
                        path: "/purchase/order",
                        query: {
                            applyOrderCode:
                                this.selectionRows[0].applyOrderCode,
                        },
                    });
                    break;
                //对应单据
                default:
                    this.$router.push({
                        path: "/documentordermanage/order",
                        query: {
                            type: type,
                            applyOrderCode:
                                this.selectionRows[0].applyOrderCode,
                        },
                    });
                    break;
            }
        },
        //反审核
        async openAntiAudit() {
            if (this.selectionRows.length == 0) {
                this.$message.error("请选择要反审核的采购订单");
                return;
            }

            if (this.selectionRows.some((p) => p.status != 8)) {
                this.$message.error("请选择已完成的订单");
                return;
            }

            const applyOrderIds = this.selectionRows.map((p) => p.applyOrderId);

            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "apply-order/anti-audit-order",
                    { applyOrderIds: applyOrderIds }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    await this.opBindData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
    },
    async mounted() {
        this.$nextTick(() => {
            this.orgType = tool.data.get("USER_INFO").AccountType;
            console.log(tool.data.get("USER_INFO"));
            this.orgName = tool.data.get("USER_INFO").OrgName;
            // if(this.orgType==1)
            // {
            //     this.columnList.push({prop:"purchaseUnityName",label:"供货单位",width:"160"});
            // }else
            // {
            //     this.columnList.push({prop:"purchaseUnityName",label:"供货单位",width:"160"});
            // }
        });

        await this.initOptions();

        this.queryList.statusList = [0, 3, 4, 5, 6, 8, 20, 21];

        var applyOrderCode = this.$route.query.applyOrderCode;
        if (applyOrderCode) {
            this.queryList.statusList = [];
            this.queryList.applyOrderCode = applyOrderCode;
        }

        await this.$nextTick();
        await this.opBindData();
    },
};
</script>
<style lang="scss" scoped>
/* 当表格行处于悬停状态时改变背景颜色 */
::v-deep .el-table .el-table__row:hover {
    background-color: #dae8f7;
}
::v-deep .el-table__body .el-table__row--striped.hover-row td {
    background-color: #dae8f7 !important;
}
//   斑马纹选中颜色
::v-deep .el-table__body .el-table__row--striped.selected-row-color td {
    background-color: #b8dcff !important;
}
::v-deep .el-table {
    --el-table-row-hover-bg-color: #dae8f7;
}
</style>
<style>
.el-table .selected-row-color {
    background-color: #b8dcff !important;
}
.el-table .selected-row-color-grey {
    color: #000000 !important;
    text-decoration: line-through; /* 删除线 */
}
.el-table--enable-row-hover .el-table__body tr.selected-row-color:hover > td {
    background-color: #b8dcff !important;
}
.el-table__body-wrapper .el-table__body tr.selected-row-color.hover-row > td {
    background-color: #b8dcff !important;
}
.el-table__fixed-body-wrapper
    .el-table__body
    tr.selected-row-color.hover-row
    > td {
    background-color: #b8dcff !important;
}
.el-table__body-wrapper
    .el-table__body
    tr.selected-row-color.hover-row
    > .el-table__row--striped {
    background-color: #b8dcff !important;
}
.el-table .cell {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
</style>
